package com.benin.sms.helpipetrpide

import android.annotation.SuppressLint
import android.content.Context
import com.benin.sms.MyApplication
import com.benin.sms.R
import com.benin.sms.valipetrpide.ValGlobalConstant


object UserStateHelp {
    val VAL_SP_KEY_TOKEN = MyApplication.context.getString(R.string.SPKeyToken)
    val VAL_SP_KEY_USER_ID = MyApplication.context.getString(R.string.SPKeyUserId)
    val VAL_SP_KEY_MOBILE = MyApplication.context.getString(R.string.SPKeyMobile)
    val VAL_SP_KEY_LOGIN_NAME = MyApplication.context.getString(R.string.SPKeyLoginName)
    val VAL_SP_KEY_READ_PERMISSION = MyApplication.context.getString(R.string.SPKeyReadPermission)


    var token: String
    var mobile: String
    var userId: String
    var loginName: String
    var agreePermissions: Boolean

    init {
        val sp = MyApplication.context.getSharedPreferences(
            ValGlobalConstant.VAL_PRODUCT,
            Context.MODE_PRIVATE
        )
        token = sp.getString(VAL_SP_KEY_TOKEN, "").toString()
        userId = sp.getString(VAL_SP_KEY_USER_ID, "").toString()
        mobile = sp.getString(VAL_SP_KEY_MOBILE, "").toString()
        loginName = sp.getString(VAL_SP_KEY_LOGIN_NAME, "").toString()
        agreePermissions = sp.getBoolean(VAL_SP_KEY_READ_PERMISSION, false)
    }

    fun isDidLogin(): Boolean {
        return token.isNotBlank() and loginName.isNotBlank()
    }

    @SuppressLint("ApplySharedPref")
    fun saveToken(gToken: String) {
        token = gToken

        val sp = MyApplication.context.getSharedPreferences(
            ValGlobalConstant.VAL_PRODUCT,
            Context.MODE_PRIVATE
        ).edit()
        sp.putString(VAL_SP_KEY_TOKEN, gToken)
        sp.commit()
    }

    @SuppressLint("ApplySharedPref")
    fun saveUserId(gUserId: String) {
        userId = gUserId

        val sp = MyApplication.context.getSharedPreferences(
            ValGlobalConstant.VAL_PRODUCT,
            Context.MODE_PRIVATE
        )
            .edit()
        sp.putString(VAL_SP_KEY_USER_ID, gUserId)
        sp.commit()
    }

    @SuppressLint("ApplySharedPref")
    fun saveMobile(gMobile: String) {
        mobile = gMobile

        val sp = MyApplication.context.getSharedPreferences(
            ValGlobalConstant.VAL_PRODUCT,
            Context.MODE_PRIVATE
        )
            .edit()
        sp.putString(VAL_SP_KEY_MOBILE, gMobile)
        sp.commit()
    }


    @SuppressLint("ApplySharedPref")
    fun saveLoginName(gLoginName: String) {
        loginName = gLoginName

        val sp = MyApplication.context.getSharedPreferences(
            ValGlobalConstant.VAL_PRODUCT,
            Context.MODE_PRIVATE
        )
            .edit()
        sp.putString(VAL_SP_KEY_LOGIN_NAME, gLoginName)
        sp.commit()
    }

    @SuppressLint("ApplySharedPref")
    fun saveAgreePermission(gAgreePermissions: Boolean) {
        agreePermissions = gAgreePermissions

        val sp = MyApplication.context.getSharedPreferences(
            ValGlobalConstant.VAL_PRODUCT,
            Context.MODE_PRIVATE
        )
            .edit()
        sp.putBoolean(VAL_SP_KEY_READ_PERMISSION, agreePermissions)
        sp.commit()
    }

    @SuppressLint("ApplySharedPref")
    fun saveTokenAndUserIdAndMobileAndAgreePermission(
        gToken: String,
        gUserId: String,
        gMobile: String,
        gLoginName: String,
        gAgreePermissions: Boolean,
    ) {
        token = gToken
        userId = gUserId
        mobile = gMobile
        loginName = gLoginName
        agreePermissions = gAgreePermissions

        val sp = MyApplication.context.getSharedPreferences(
            ValGlobalConstant.VAL_PRODUCT,
            Context.MODE_PRIVATE
        )
            .edit()
        sp.putString(VAL_SP_KEY_TOKEN, gToken)
        sp.putString(VAL_SP_KEY_USER_ID, gUserId)
        sp.putString(VAL_SP_KEY_MOBILE, gMobile)
        sp.putString(VAL_SP_KEY_LOGIN_NAME, gLoginName)
        sp.putBoolean(VAL_SP_KEY_READ_PERMISSION, agreePermissions)
        sp.commit()
    }

    fun logout() {
        saveTokenAndUserIdAndMobileAndAgreePermission("", "", "", "", agreePermissions)
    }
}